import {
  getGroupList
} from '../../apis/group';
import {
  globalData
} from '../../utils/request';
const app = getApp();

Page({
  data: {
    nav: 1,
    officialRequest: false,
    officialList: [],
    officialTotal: 0,
    officialListHaveMore: true,
    officialPageNum: 1,
    userRequest: false,
    userList: [],
    userTotal: 0,
    userListHaveMore: true,
    userPageNum: 1,
    isSearch: false,
    inputValue: '',
    officialSearchList: [],
    userSearchList: []
  },

  onLoad() {
    this.setData({
      showGroupBtn: globalData.checkIn
    });
  },

  onShow() {
    this.setData({
      officialPageNum: 1,
      officialListHaveMore: true,
      userPageNum: 1,
      userListHaveMore: true
    });
    this.getOfficialGroupList(1, 10, 'refresh');
    this.getUserGroupList(1, 10, 'refresh');
  },

  onReachBottom() {
    if (this.data.nav === 1 && this.data.officialListHaveMore) {
      this.setData({
        officialPageNum: this.data.officialPageNum + 1
      });
      this.getGroupList(this.data.officialPageNum, 10);
    } else if (this.data.nav === 2 && this.data.userListHaveMore) {
      this.setData({
        userPageNum: this.data.userPageNum + 1
      });
      this.getGroupList(this.data.userPageNum, 10);
    };
  },

  getOfficialGroupList(pageNum, pageSize, type) {
    if (this.data.officialRequest) return
    this.setData({
      officialRequest: true
    });
    getGroupList(1, pageNum, pageSize).then(res => {
      this.setData({
        officialRequest: false
      });
      if (res.data.code === 200) {
        let {
          rows,
          total
        } = res.data;
        rows.forEach(v => {
          if (!new RegExp('https://').test(v.image) && !new RegExp('http://').test(v.image) && v.image !== '') v.image = app.globalData.url + v.image;
        });
        if (type === 'refresh') {
          this.setData({
            officialList: rows,
            officialTotal: total
          });
        } else {
          rows.forEach(v => {
            this.data.officialList.push(v);
          });
          this.setData({
            officialList: this.data.officialList,
            officialTotal: total
          });
        };
        if (this.data.officialList.length === total) {
          this.setData({
            officialListHaveMore: false
          });
        };
      };
    });
  },
  getUserGroupList(pageNum, pageSize, type) {
    if (this.data.userRequest) return
    this.setData({
      userRequest: true
    });
    getGroupList(2, pageNum, pageSize).then(res => {
      this.setData({
        userRequest: false
      });
      if (res.data.code === 200) {
        let {
          rows,
          total
        } = res.data;
        rows.forEach(v => {
          if (!new RegExp('https://').test(v.image) && !new RegExp('http://').test(v.image) && v.image !== '') v.image = app.globalData.url + v.image;
        });
        if (type === 'refresh') {
          this.setData({
            userList: rows,
            userTotal: total
          });
        } else {
          rows.forEach(v => {
            this.data.userList.push(v);
          });
          this.setData({
            userList: this.data.userList,
            userTotal: total
          });
        };
        if (this.data.userList.length === total) {
          this.setData({
            userListHaveMore: false
          });
        };
      };
    });
  },

  // 导航栏
  changeNav(e) {
    let {
      type
    } = e.currentTarget.dataset;
    this.setData({
      nav: type,
      inputValue: '',
      isSearch: false
    });
    if (type === 1 && this.data.officialList.length === 0) {
      this.getOfficialGroupList(1, 10);
    } else if (type === 2 && this.data.userList.length === 0) {
      this.getUserGroupList(1, 10);
    };
  },

  // 创建群
  createGroup() {
    wx.navigateTo({
      url: '/pages/myCard/index?type=createGroup'
    });
  },

  // 查看群
  goGroup(e) {
    let {
      id
    } = e.currentTarget.dataset;
    wx.setStorageSync('selectGroup', JSON.stringify({
      groupId: id
    }));
    wx.switchTab({
      url: '/pages/groupDetails/index'
    });
  },

  // 输入框搜索
  handleInput(e) {
    let {
      value
    } = e.detail, arr = new Array;

    if (value === '') {
      this.setData({
        isSearch: false,
        inputValue: '',
        officialSearchList: [],
        userSearchList: []
      });
    } else {
      this.setData({
        inputValue: value
      });
      if (this.data.nav == 1) {
        this.data.officialList.forEach(v => {
          if (new RegExp(`${value}`).test(v.title)) {
            arr.push(v);
          };
        });
        this.setData({
          isSearch: true,
          officialSearchList: arr
        });
      } else {
        this.data.userList.forEach(v => {
          if (new RegExp(`${value}`).test(v.title)) {
            arr.push(v);
          };
        });
        this.setData({
          isSearch: true,
          userSearchList: arr
        });
      };
    };
  }
})